const Router = require('koa-router');
const Survey = require('../models/survey.cjs');
const { saveSurvey, loadSurveys } = require('../utils/storage.cjs');
const { authenticateUser } = require('./auth.cjs');

const router = new Router();

// 通过问卷链接访问问卷内容（用于填写问卷页面）
router.get('/api/surveys/link/:id', async (ctx) => {
  try {
    const surveys = await loadSurveys();
    const survey = surveys[ctx.params.id];
    console.log("后端通过链接得到的问卷",survey);
    if (!survey) {
      ctx.throw(404, '问卷不存在');
    }

    if (survey.status !== 'published') {
      ctx.throw(403, '问卷未发布，无法访问');
    }

    ctx.status = 200;
    ctx.body = {
      success: true,
      message:'通过链接获取问卷',
      data: survey
    };
  } catch (error) {
    ctx.status = error.status || 500;
    ctx.body = {
      success: false,
      message: error.message
    };
  }
});

router.post('/api/surveys', authenticateUser, async (ctx) => {
  try {
    const surveyData = {
      ...ctx.request.body,
    };

    const survey = new Survey(surveyData);
    await saveSurvey(survey);
    console.log("提交创建问卷",survey);
    
    ctx.status = 201;
    ctx.body = {
      success: true,
      data: survey
    };
  } catch (error) {
    ctx.status = 400;
    ctx.body = {
      success: false,
      message: error.message
    };
  }
});

router.get('/api/surveys/:email', authenticateUser, async (ctx) => {
  try {
    const surveys = await loadSurveys();
    const userSurveys = Object.values(surveys).filter(
      survey => survey.creatorId === ctx.params.email
    );

    ctx.status = 200;
    ctx.body = {
      success: true,
      data: userSurveys
    };
  } catch (error) {
    ctx.status = 404;
    ctx.body = {
      success: false,
      message: '获取问卷列表失败：' + error.message
    };
  }
});

router.put('/api/surveys/:id/publish', authenticateUser, async (ctx) => {
  try {
    const surveys = await loadSurveys();
    const survey = surveys[ctx.params.id];
    
    
    if (!survey) {
      ctx.throw(404, '问卷不存在');
    }
    

    if (survey.status !== 'draft') {
      ctx.throw(400, '只有草稿状态的问卷可以发布');
    }

    survey.status = 'published';
    survey.publishedAt = new Date().toISOString();
    await saveSurvey(survey);

    ctx.body = {
      success: true,
      link: `https://localhost:5173/surveys/${ctx.params.id}`
    };
  } catch (error) {
    ctx.status = error.status || 500;
    ctx.body = {
      success: false,
      message: error.message
    };
  }
});

router.put('/api/surveys/:id/close', authenticateUser, async (ctx) => {
  try {
    const surveys = await loadSurveys();
    const survey = surveys[ctx.params.id];

    if (!survey) {
      ctx.throw(404, '问卷不存在');
    }

    if (survey.status !== 'published') {
      ctx.throw(400, '只有已发布的问卷可以关闭');
    }

    survey.status = 'closed';
    await saveSurvey(survey);

    ctx.body = {
      success: true,
      message: '问卷已成功关闭'
    };
  } catch (error) {
    ctx.status = error.status || 500;
    ctx.body = {
      success: false,
      message: error.message
      
    };
  }
});

//无限制状态通过id访问问卷，用于渲染到查看我的填写记录页面
router.get('/api/any/surveys/:id', async (ctx) => {

  
  try {
    const surveys = await loadSurveys();
    const survey = surveys[ctx.params.id];
   

    if (!survey) {
      ctx.throw(404, '问卷不存在');
    }

    ctx.status = 200;
    ctx.body = {
      success: true,
      message: '无状态限制获取问卷成功',
      data: survey
    };
  } catch (error) {
    ctx.status = error.status || 500;
    ctx.body = {
      success: false,
      message: error.message
    };
  }
});



module.exports = router;